package com.yqg.protection.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.protection.common.Result;
import com.yqg.protection.common.interceptor.SysLog;
import com.yqg.protection.entity.Agreement;
import com.yqg.protection.mapper.AgreementMapper;
import com.yqg.protection.service.IAgreementService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * AgreementController 前端控制器
 * @author zxf
 * @since 2024-09-11
 */
@RestController
@RequestMapping("/agreement")
public class AgreementController {

    @Resource
    private IAgreementService agreementService;

    @Resource
    private AgreementMapper agreementMapper;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    @SysLog("新增或者更新协议")
    public Result save(@RequestBody Agreement agreement) {
        agreement.setIsDelete(0);
        agreement.setCreateTime(LocalDateTime.now());
        return Result.success(agreementService.saveOrUpdate(agreement));
    }
    /**
    *  删除
    */
    @GetMapping("/delete/{id}")
    @SysLog("删除协议")
    public Result delete(@PathVariable Integer id) {
        Agreement agreement =agreementService.getById(id);
        agreement.setIsDelete(1);
        return Result.success(agreementService.saveOrUpdate(agreement));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<Agreement> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
       List<Agreement> list=  agreementService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(agreementService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String name) {
        pageNum = pageNum * pageSize - pageSize;
        List<Agreement> agreements = agreementMapper.findPage(pageNum, pageSize, name);
        Long count = agreementMapper.findPageCount(name);
        HashMap<String, Object> map = new HashMap<>();
        map.put("record", agreements);
        map.put("total", count);
        return Result.success(map);
    }

    @ApiOperation("导出数据")
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       String name) {
        agreementService.export(response, name);
    }

    @ApiOperation("导入数据")
    @PostMapping("/importData")
    @SysLog("导入协议")
    public void importData(MultipartFile file) {
        agreementService.importData(file);
    }
}

